#include "Plane.h"

Plane::Plane() :
m_d(0.0f)
{}

Plane::Plane(const Vec3& norm, const float& d) :
m_norm(norm),
    m_d(d)
{}

Plane::Plane(const float& a, const float& b, 
    const float& c, const float& d) : 
m_norm(a,b,c),
    m_d(d)
{}

Plane::Plane(const Vec3& v1, const Vec3& v2, const Vec3& v3)
{
    m_norm = ((v2-v1).CrossProduct(v3-v2)).normalise();
    m_d = v1.DotProduct(m_norm);
}

void Plane::CreateFromPoints(const Vec3& v1, const Vec3& v2, const Vec3& v3)
{
    Vec3 temp1, temp2;
    temp1 = v1 - v2;
    temp2 = v3 - v2;

    m_norm = temp2.CrossProduct(temp1);
    m_norm.normalise();
    m_d = -(m_norm.DotProduct(v2));
}

void Plane::Normalise()
{
    if(m_norm.squareMagnitude() > 1)
        m_norm.normalise();
    m_d /= m_norm.magnitude();
}

float Plane::Distance(const Vec3& q) const
{
    return (m_d + m_norm.DotProduct(q));
}

const Vec3& Plane::GetNormal() const
{
    return m_norm;
}

const float& Plane::GetOffset() const
{
    return m_d;
}